package com.itcast.googleplay.fragment;

import java.util.List;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.itcast.googleplay.R;
import com.itcast.googleplay.adapter.MyBaseAdapter;
import com.itcast.googleplay.bean.SubjectInfo;
import com.itcast.googleplay.holder.BaseHolder;
import com.itcast.googleplay.holder.SubjectHolder;
import com.itcast.googleplay.protocol.SubjectProtocol;
import com.itcast.googleplay.ui.LoadingPage.ResultState;
import com.itcast.googleplay.utils.UIUtils;


public class SubjectFragment extends BaseFragment {

	private ListView listView;
	private List<SubjectInfo> subjectData;
	private MySubjectAdapter mySubjectAdapter;
	private PullToRefreshListView pullToRefreshListView;

	@Override
	public ResultState onSubLoad() {
		SubjectProtocol subjectProtocol = new SubjectProtocol();
		subjectData = subjectProtocol.getData("subject", 0, "");
		if(subjectData!=null){
			if(subjectData.size() == 0){
				return ResultState.STATE_EMTPY;
			}
			return ResultState.STATE_SUCCESSED;
		}
		return ResultState.STATE_ERROR;
	}

	@Override
	public View onCreateSuccessedSubView() {
		View view = UIUtils.inflate(R.layout.layout_refresh_list);
		pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.pull_refresh_list);
		listView = pullToRefreshListView.getRefreshableView();
		listView.setDivider(null);
		
		initData();
		
		pullToRefreshListView.setMode(Mode.BOTH);
		
		pullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				refreshOrLoadMore();
			}
		});
		return view;
	}

	protected void refreshOrLoadMore() {
		if(pullToRefreshListView.getCurrentMode() == Mode.PULL_FROM_START){
			//刷新
			subjectData.clear();
			showData();
			
			pullToRefreshListView.onRefreshComplete();
		}else{
			//加载更多
			new Thread(){
				public void run() {
					SubjectProtocol subjectProtocol = new SubjectProtocol();
					List<SubjectInfo> moreData = subjectProtocol.getData("subject", subjectData.size(), "");
					if(subjectData!=null && moreData!=null && moreData.size()>0){
						
						subjectData.addAll(moreData);
						UIUtils.runInMainThread(new Runnable() {
							@Override
							public void run() {
								mySubjectAdapter.notifyDataSetChanged();
								pullToRefreshListView.onRefreshComplete();
							}
						});
					}else{
						UIUtils.runInMainThread(new Runnable() {
							@Override
							public void run() {
								Toast.makeText(UIUtils.getContext(),"没有数据了......", 0).show();
								pullToRefreshListView.onRefreshComplete();
							}
						});
					}
				};
			}.start();
		}
	}

	private void initData() {
		mySubjectAdapter = new MySubjectAdapter(subjectData);
		listView.setAdapter(mySubjectAdapter);
	}
	
	class MySubjectAdapter extends MyBaseAdapter<SubjectInfo>{
		public MySubjectAdapter(List<SubjectInfo> list) {
			super(list);
		}
		@Override
		public BaseHolder<SubjectInfo> getHolder() {
			return new SubjectHolder();
		}
	}
}
